package com.suray.rgv.son;

import com.suray.basic.wcs.path.core.Node;
import com.suray.rgv.RgvSonInterface;
import com.suray.rgv.son.core.RgvSonCore;
import com.suray.rgv.son.core.RgvSonInfo;
import com.suray.rgv.son.util.RgvSonDirection;
import com.suray.rgv.util.ErrorInfo;
import com.suray.type.RgvSonTaskResult;
import com.suray.type.RgvSonTaskStatus;
import com.suray.wcs.res.enums.Order;
import com.suray.wcs.res.enums.PalletStatus;
import com.suray.wcs.res.enums.RgvStatus;
import com.suray.wcs.res.enums.RgvType;

import java.io.IOException;
import java.util.List;

/**
 * 子车实体，实现接口RgvSonInterface
 */
public class RgvSonImpl implements RgvSonInterface {

    private RgvSonCore rgvSonCore;

    private Object mapObj;

    public RgvSonImpl(int rgvNo, String serialNum, String ip, int port) {
        rgvSonCore = new RgvSonCore(rgvNo, serialNum);
        initNet(ip, port);
        rgvSonCore.start();
    }

    public void run() {

    }

    @Override
    public void init() {
        rgvSonCore.init();
    }

    @Override
    public void initNet(String ip, int port) {
        rgvSonCore.initSocket(ip, port);
    }

    @Override
    public String getSeries() {
        return rgvSonCore.getSeries();
    }

    @Override
    public boolean task(Object taskId, List<Node> pathNodes) {
        return false;
    }

    @Override
    public boolean isTaskEnd(int taskId) {
        throw new RuntimeException("子车不支持改接口，请改用boolean isTaskEnd(Object taskId)");
    }

    @Override
    public boolean isTaskEnd(Object taskId) {
        return rgvSonCore.checkTaskEnd(taskId);
    }

    @Override
    public boolean isInMother() {
        return rgvSonCore.isInMother();
    }

    @Override
    public void changeSpeedConfig(Integer moveSpeed, Integer moveSpeedAcc, Integer moveSpeedDec) {
        rgvSonCore.changeSpeedConfig(moveSpeed, moveSpeedAcc, moveSpeedDec);
    }

    @Override
    public void setRgvDirectionX(Boolean rgvDirection) {
        rgvSonCore.setRgvDirectionX(rgvDirection);
    }

    @Override
    public void setRgvDirectionY(Boolean rgvDirection) {
        rgvSonCore.setRgvDirectionY(rgvDirection);
    }

    @Override
    public String getSonGroundCodeInMother() {
        return rgvSonCore.getSonGroundCodeInMother();
    }

    public void setPlatformGroundCode(String platformGroundCode) {
        rgvSonCore.setPlatformGroundCode(platformGroundCode);
    }

    @Override
    public boolean checkSonRgvStatusCanDoTask() {
        return rgvSonCore.checkSonRgvStatusCanDoTask();
    }

    @Override
    public boolean isSonRgvNoTask() {
        return rgvSonCore.checkFree();
    }

    @Override
    public Integer getCurActionNo() {
        return rgvSonCore.getCurActionNo();
    }

    @Override
    public void checkException() {
        rgvSonCore.checkException();
    }

    @Override
    public boolean isDataInfoOutTime() {
        return rgvSonCore.isDataInfoOutTime();
    }

    @Override
    public boolean isRgvEnable() {
        return rgvSonCore.isRgvEnable();
    }

    @Override
    public void enableRgv() {
        rgvSonCore.enableRgv();
    }

    @Override
    public void disableRgv() {
        rgvSonCore.disableRgv();
    }

    @Override
    public int getErrorCode() {
        return rgvSonCore.getErrorCode();
    }

    @Override
    public int getWarnCode() {
        return rgvSonCore.getWarnCode();
    }

    @Override
    public long getRgvSonDataTime() {
        return rgvSonCore.getRgvSonDataTime();
    }

    @Override
    public synchronized boolean contactObject(Object obj) {
        if (mapObj == null) {
            mapObj = obj;
            return true;
        } else if (mapObj.equals(obj)) {
            return true;
        }

        return false;
    }

    @Override
    public synchronized boolean clearContact(Object obj) {
        if (mapObj == null) {
            return true;
        } else if (mapObj.equals(obj)) {
            mapObj = null;
            return true;
        }

        return false;
    }

    @Override
    public Object getContactObject() {
        return mapObj;
    }

    @Override
    public boolean isActionEnd(Order action) {
        return false;
    }

    @Override
    public Integer getTaskId() {
        return null;
    }

    @Override
    public int getTaskNo() {
        return 0;
    }

    @Override
    public boolean action(Order rgvActionType) {
        return false;
    }

    @Override
    public boolean action(Order rgvActionType, Object actionParam) {
        return false;
    }

    @Override
    public boolean changeLocation(Node node) {
        return false;
    }

    @Override
    public ErrorInfo getErrorInfo() {
        return null;
    }

    @Override
    public List<ErrorInfo> getErrorInfos() {
        return null;
    }

    @Override
    public void clearError(ErrorInfo error) {

    }

    @Override
    public void clearAllError() {

    }

    @Override
    public boolean recovery() throws IOException {
        return rgvSonCore.recovery();
    }

    @Override
    public Node getLocation() {
        return null;
    }

    @Override
    public RgvStatus getRgvStatus() {
        return rgvSonCore.getRgvStatus();
    }

    @Override
    public PalletStatus getRgvPalletStatus() {
        return rgvSonCore.getRgvPalletStatus();
    }

    @Override
    public Float getBatSoc() {
        return rgvSonCore.getBatSoc();
    }

    @Override
    public Float getBatTemp() {
        return null;
    }

    @Override
    public Float getBatVolt() {
        return null;
    }

    @Override
    public Float getBatCurrent() {
        return null;
    }

    @Override
    public Integer getBatChargeNum() {
        return null;
    }

    @Override
    public Float getAutoChargeBattery() {
        return null;
    }

    @Override
    public Integer getForceChargeBattery() {
        return null;
    }

    @Override
    public boolean isForbidden() {
        return false;
    }

    @Override
    public boolean isAutoCharge() {
        return false;
    }

    @Override
    public boolean isRgvValid() {
        return false;
    }

    @Override
    public String getNetId() {
        return null;
    }

    @Override
    public String getMap() {
        return null;
    }

    @Override
    public int getRgvNo() {
        return 0;
    }

    @Override
    public boolean stopTask() {
        return false;
    }

    @Override
    public void cancelTask(int taskId) {

    }

    @Override
    public boolean emergencyStop() {
        return false;
    }

    @Override
    public RgvType getRgvType() {
        return null;
    }

    @Override
    public void sendMap(String wareHouse) throws IOException {

    }

    @Override
    public String downloadMap(String mapName) throws IOException {
        return null;
    }

    @Override
    public void deleteMap(String mapName) throws IOException {

    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isOnline() {
        return rgvSonCore.isOnline();
    }

    @Override
    public void setInterrupt() throws IOException {

    }

    @Override
    public void rgvError(ErrorInfo errorInfo) {
        rgvSonCore.rgvError(errorInfo);
    }

    @Override
    public void forwardRun() {

    }

    @Override
    public void fallBackRun() {

    }

    @Override
    public void turnLeft() {

    }

    @Override
    public void turnRight() {

    }

    @Override
    public void stopManualMove() {

    }

    @Override
    public boolean isCharging() {
        return false;
    }

    @Override
    public boolean isStatic() {
        return rgvSonCore.isStatic();
    }

    @Override
    public boolean positionOk() {
        return rgvSonCore.positionOk();
    }

    @Override
    public void destroy() {
        rgvSonCore.endThread();
    }

    @Override
    public void sendTask(Object taskId, int rgvSonTaskId, List<Node> nodeList, int[] pathIndexs, boolean startTask)
            throws IOException {
        rgvSonCore.sendTask(taskId, rgvSonTaskId, nodeList, pathIndexs, startTask);
    }

    @Override
    public void sendSegment(int segment) throws IOException {
        rgvSonCore.generalSendSegmentScript(segment);
    }

    @Override
    public void setRgvSonSpeedScript() throws IOException {
        rgvSonCore.setRgvSonSpeedScript();
    }

    @Override
    public int getRgvTaskEndSegment() {
        return rgvSonCore.getRgvTaskEndSegment();
    }

    @Override
    public int getRgvCurTaskId() {
        return rgvSonCore.getRgvCurTaskId();
    }

    @Override
    public RgvSonTaskStatus getRgvSonTaskStatus() {
        return rgvSonCore.getRgvSonTaskStatus();
    }

    @Override
    public RgvSonTaskResult getRgvSonTaskResult() {
        return rgvSonCore.getRgvSonTaskResult();
    }

    @Override
    public int getRgvSonTaskErrorCode() {
        return rgvSonCore.getRgvSonTaskErrorCode();
    }

    @Override
    public int getActionResult() {
        return rgvSonCore.getActionResult();
    }

    @Override
    public void calibration(Object taskId) throws IOException {
        rgvSonCore.calibration(taskId);
    }

    @Override
    public void moveSpeedAction(Object taskId, Integer direction) throws IOException {
        rgvSonCore.moveSpeedAction(taskId, RgvSonDirection.parseDirection(direction));
    }

    @Override
    public void stopMoveBySpeed(String taskId) throws IOException {
        rgvSonCore.stopMoveSpeedAction(taskId);
    }

    @Override
    public void palletUpBySpeed(Object taskId, boolean up) throws IOException {
        rgvSonCore.palletUpBySpeed(taskId, up);
    }

    @Override
    public void charge(Object taskId) throws IOException {
        rgvSonCore.startCharge(taskId);
    }

    @Override
    public void stopCharge(Object taskId) throws IOException {
        rgvSonCore.stopCharge(taskId);
    }

    @Override
    public void moveByDistance(Object taskId, Integer direction) throws IOException {
        rgvSonCore.moveByDistance(taskId, RgvSonDirection.parseDirection(direction));
    }

    @Override
    public void clearMoveOdo(Object taskId) throws IOException {
        rgvSonCore.clearMoveOdo(taskId);
    }

    @Override
    public void clearPalletOdo(Object taskId) throws IOException {
        rgvSonCore.clearPalletOdo(taskId);
    }

    @Override
    public void palletCalibration(Object taskId) throws IOException {
        rgvSonCore.palletCalibration(taskId);
    }

    @Override
    public void clearTask(Object taskId) throws IOException {
        rgvSonCore.clearTask(taskId);
    }

    @Override
    public void actionResultConfirm() throws IOException {
        rgvSonCore.actionResultOk();
    }

    @Override
    public void clearError() throws IOException {
        rgvSonCore.clearError();
    }

    @Override
    public void taskStart() throws IOException {
        rgvSonCore.taskStart();
    }

    @Override
    public void clearTask() throws IOException {
        rgvSonCore.actionResultOk();
    }

    @Override
    public void palletUp(Object taskId) throws IOException {
        rgvSonCore.palletUp(taskId);
    }

    @Override
    public void palletDown(Object taskId) throws IOException {
        rgvSonCore.palletDown(taskId);
    }

    @Override
    public String getGroundCode() {
        return rgvSonCore.getGroundCode();
    }

    /**
     * 小车方向
     * y增大方向或x增大方向
     * 为右：true
     * 为左：false
     */
    public RgvSonInfo getRgvSonInfoData() {
        return rgvSonCore.getRgvSonInfoData();
    }
}
